package com.xz.spi.jdbc;

import org.apache.commons.lang3.StringUtils;

import java.util.NoSuchElementException;
import java.util.ServiceLoader;

/**
 * Created by fingal on 2018/1/7.
 */
public class DriverManager {
    public Driver getDriver(String url) {
        if(StringUtils.isBlank(url)) {
            throw new IllegalArgumentException("url cannot be empty...");
        }

        ServiceLoader<Driver> serviceLoader = ServiceLoader.load(Driver.class);
        for (Driver driver : serviceLoader) {
            if(driver.acceptsURL(url)) {
                return driver;
            }
        }

        throw new NoSuchElementException(String.format("driver[%s] not found...", url));
    }
}
